#!/usr/bin/python
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.proxy import Proxy, ProxyType
import time

PAGE_LINK = 'https://jira.inside.nokiasiemensnetworks.com/browse/XTEAM'
LOG_NAME = 'lixzhou'
LOG_PASSWORD = 'TingYuan66'

myProxy = "10.144.1.10:8080"

proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': myProxy,
    'ftpProxy': myProxy,
    'sslProxy': myProxy,
    'noProxy': '' # set this value as desired
    })

def report_jira_case():
    driver = webdriver.Firefox()
    driver.get(PAGE_LINK)
    driver.maximize_window()
    '''new open page may need to login'''
    login(driver)
    '''open issue div'''
    open_issue_div(driver)
    #driver.switch_to_frame('create-issue-dialog')
    '''fill issue list and submit'''
    fill_issue_list(driver)
    

def login(page_driver):
    txt_logname = page_driver.find_element_by_id('login-form-username')
    txt_logpswd = page_driver.find_element_by_id('login-form-password')
    btn_submit = page_driver.find_element_by_id('login-form-submit')
    if not txt_logname:
        return 
    txt_logname.send_keys(LOG_NAME)
    txt_logpswd.send_keys(LOG_PASSWORD)
    btn_submit.click()
    page_driver.implicitly_wait(10)
    
def open_issue_div(page_driver):
    lbl_createissue = page_driver.find_element_by_id('create_link')
    lbl_createissue.click()
    
def fill_issue_list(page_driver):
    #cb_project = page_driver.find_element_by_id('issuetype-field')
    cb_project = page_driver.find_element_by_id('issuetype')
    #288 is the value of option 'Problem from CRT'
    e = cb_project.find_element_by_xpath("//option[@value='288']")
    e.style.display = 'block'
    print e
    e.click()
    
def webdriver_phantomjs():
    driver = webdriver.PhantomJS()
    driver.get('http://pan.baidu.com/disk/home#from=share_pan_logo')
    print driver.page_source
#     data = driver.find_element_by_id('cp').text
#     print data

def logon():
#     test_link = 'http://10.56.117.81/coci/test/policy_list?test_phase=crt&term=104'
    test_link = 'http://pan.baidu.com/'
    driver = webdriver.PhantomJS()
#     proxy.add_to_capabilities(driver.desired_capabilities)
#     print driver.desired_capabilities
    driver.get(test_link)
#     driver.implicitly_wait(15000)
    print 'start'
    time.sleep(30)
    print 'stop'
    print driver.page_source
    print driver.get_screenshot_as_file('pic.png')
#     print 'by tag', driver.find_element_by_name('userName')
#     res1 = driver.find_element_by_id('header-login')
#     print 'res1',res1
#     res2 = driver.find_element_by_class_name('tang-pass-login')
#     print 'res2',res2
    print 'find form:', driver.find_elements_by_id('TANGRAM__PSP_4__userName')
#     print 'by class', driver.find_element_by_class_name('pass-text-input pass-text-input-userName')
#     print 'by class', driver.find_element(By.CSS_SELECTOR,'pass-text-input pass-text-input-userName')
#     print 'by class', driver.find_element_by_css_selector('.pass-text-input.pass-text-input-userName')
#     print 'by class', driver.find_elements_by_css_selector('pass-text-input.pass-text-input-userName')
#     print 'by class', driver.find_element_by_xpath("input[@class='pass-text-input pass-text-input-userName']")
#     '''collect all cookies'''
#     cookies = driver.get_cookies()
#     print type(cookies)
#     for cookie in cookies:
#         print  cookie
#     print driver.find_elements_by_class_name('tang-pass-login')
#     res = driver.find_element_by_id('netdisk_pass_login_form')
#     print res
#     time.sleep(3)

def ci_web():
#     test_link = 'http://10.56.117.81/coci/test/policy_list?test_phase=crt&term=104'
    test_link = 'http://pan.baidu.com/'
    driver = webdriver.PhantomJS()
#     proxy.add_to_capabilities(driver.desired_capabilities)
#     print driver.desired_capabilities
    driver.get(test_link)
#     driver.implicitly_wait(15000)
    time.sleep(20)
#    print driver.get_screenshot_as_file('pic.png')
    print 'get element:'
    seq = 0
    def add_num(i):
        return seq + 1 
#    print 'element:%d'%add_num(seq), driver.find_element_by_id('myhtml')
#    print 'element:%d'%add_num(seq), driver.find_element_by_id('login-body')
#    print 'element:%d'%add_num(seq), driver.find_element_by_class_name('header-container')
#    print 'element:%d'%add_num(seq), driver.find_element_by_id('login-middle')
#    print 'element:%d'%add_num(seq), driver.find_element_by_class_name('header-login')
    print 'element:%d'%add_num(seq), driver.find_element_by_id('netdisk_pass_login_form')
    
#    print 'element:%d'%add_num(seq), driver.find_element_by_class_name('pass-form')
    #'''''''''''
#    print 'element:%d'%add_num(seq), driver.find_element_by_id('TANGRAM__PSP_4__userNameWrapper')
#    print 'element:%d'%add_num(seq), driver.find_element_by_id('TANGRAM__PSP_4__userName')
#    print 'element:%d'%add_num(seq), driver.find_element_by_id('pass-form')
#    print 'element:%d'%add_num(seq), driver.find_element_by_id('pass-form')
#    print 'element:%d'%add_num(seq), driver.find_element_by_id('pass-form')
#    print 'element:%d'%add_num(seq), driver.find_element_by_id('pass-form')
#    
#     
def get_all_element_by_type():
    test_link = 'http://pan.baidu.com/'
    driver = webdriver.PhantomJS()
    driver.get(test_link)
    time.sleep(20)
    res = driver.find_elements_by_tag_name('div')
    print 'all form', len(res)
    
def analys_page(url):
    try:
        driver = webdriver.PhantomJS()
        driver.get
if __name__ == "__main__":
    get_all_element_by_type()